package com.open.capacity.workflow.controller;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.workflow.model.WFSchemeAuthorize;
import com.open.capacity.workflow.model.WFSchemeinfo;
import com.open.capacity.workflow.service.WFSchemeAuthorizeService;
import com.open.capacity.workflow.service.WFSchemeService;
import com.open.capacity.workflow.vo.SchemeVo;
import com.open.capacity.workflow.vo.WorkFlowSchemeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 作者 ftquan 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 流程
 */
@RestController
@Api(tags = "流程模板模块api")
public class WFSchemeController {

    @Autowired
    private WFSchemeService schemeService;

    @Autowired
    private WFSchemeAuthorizeService authorizeService;

    /**
     * 查询流程
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('wfscheme:get/wfschemes')")
    @ApiOperation(value = "查询流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/wfschemes")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public PageResult<WFSchemeinfo> findSchemes(@RequestParam Map<String, Object> params) {
        return schemeService.pageSchemeinfos(params);
    }


    /**
     * 查询流程list
     *
     * @param parentId
     * @return
     */
    @PreAuthorize("hasAuthority('wfscheme:get/wfschemes')")
    @ApiOperation(value = "查询流程list")
    @GetMapping("/wfschemes/list")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public Result<List<WFSchemeinfo>> listSchemes(@RequestParam(value = "parentId", defaultValue = "0") String parentId, @RequestParam(value = "keyword", required = false) String keyword) {
        Map<String, Object> params = new HashMap<>();
        params.put("parentId", parentId);
        params.put("keyword", keyword);
        List<WFSchemeinfo> list = schemeService.findSchemeinfos(params);
        return Result.succeed(list);
    }


    @ApiOperation(value = "获取流程")
    @PreAuthorize("hasAuthority('wfscheme:get/wfschemes/{id}')")
    @GetMapping("/wfschemes/{id}")
    public Result<SchemeVo> findSchemeById(@PathVariable(value = "id") String id) {
        SchemeVo schemeinfo = schemeService.findById(id);
        List<WFSchemeAuthorize> authorizes = authorizeService.findBySchemeinfoId(id);
        schemeinfo.setShcemeAuthorizes(authorizes);
        return Result.succeed(schemeinfo);
    }

    /**
     * 更新流程状态
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('wfscheme:post/wfschemes/{id}/updateStatus' )")
    @PostMapping(value = "/wfschemes/{id}/updateStatus")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public Result updateStatus(@PathVariable(value = "id") String id, Integer status) {
        schemeService.updateStatus(id, status);
        return Result.succeed(null, "更新成功");
    }

    /**
     * 流程新增或者更新
     *
     * @param scheme
     * @return
     */
    @PreAuthorize("hasAnyAuthority('wfscheme:post/wfschemes','wfscheme:put/wfschemes')")
    @ApiOperation(value = "保存流程")
    @PostMapping("/wfschemes/saveOrUpdate")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public Result saveOrUpdate(@RequestBody WorkFlowSchemeVo scheme) {
        return schemeService.saveOrUpdate(scheme);
    }


    /**
     * 删除
     * delete /wfschemes/1
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('wfscheme:delete/wfschemes/{id}')")
    @ApiOperation(value = "删除流程")
    @DeleteMapping("/wfschemes/{id}")
    @LogAnnotation(name = "流程",module = "workflow-center", recordRequestParam = false)
    public Result delete(@PathVariable(value = "id") String id) {
        try {
            schemeService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("操作失败");
        }
    }

}
